var $table = $('#addTo-index-goods');

//参数
var parm = [];


$(function () {
    //初始化
    var url = urlcore + "/api/experienceRecommend/selectNotList";
    initGroupTable(url);
});


//初始化表格
function initGroupTable(url) {
    $table.bootstrapTable({
        columns: [{
            field: "id",
            title: "序号",
            formatter: function (a, b, c) {
                return c + 1;
            }
        }, {
            field: "title",
            title: "标题"
        }, {
            field: "id",
            title: "推荐人",
            formatter: function (a, b, c) {
                return b.user.userName;
            }
        }, {
            field: "checkStatus",
            title: "审核状态",
            formatter: function (a, b, c) {
                if (a == 1) {
                    return "审核通过";
                } else if (b.checkStatus == 2) {
                    return "未审核";
                } else if(b.checkStatus == 3){
                    return "未通过";
                }else{
                    return "前端移除";
                }
            }
        }, {
            field: "isHome",
            title: "是否加精",
            formatter: function (a, b, c) {
                if (a == 1) {
                    return "是";
                } else {
                    return "否";
                }
            }
        },  {
            field: "isUp",
            title: "是否置顶",
            formatter: function (a, b, c) {
                if (a == 1) {
                    return "是";
                } else {
                    return "否";
                }
            }
        }, {
            field: "gmtDatetime",
            title: "提交时间"
        }, {
            field: "id",
            title: "操作",
            formatter: function (a, b, c) {
                var ps = "";
                var sj = "";
                var rmStr="";
                var delStr = "<a class=\"btn btn-danger btn-xs\" href=\"javascript:;\" onclick=\"doDelete(" + b.id + ")\"> 删除</a>";
                if (b.checkStatus == 1) {
                    if (b.isHome == 0) {
                        sj+= "&nbsp;&nbsp;<a class=\"btn btn-info btn-xs\" href=\"javascript:;\" onclick=\"update(" + b.id + ",null,"  + 1 +",null"+ ")\"> 加精</a>";
                    }
                    if (b.isHome == 1 ) {
                        sj += "&nbsp;&nbsp;<a class=\"btn btn-info btn-xs\" href=\"javascript:;\" onclick=\"update(" + b.id + ",null," + 0 + "," + 0 + ")\"> 取消加精</a>";
                        if(b.isUp==0){
                            sj+= "&nbsp;&nbsp;<a class=\"btn btn-info btn-xs\" href=\"javascript:;\" onclick=\"update(" + b.id + ",null,null,"+1+ ")\"> 置顶</a>";
                        }else{
                            sj+= "&nbsp;&nbsp;<a class=\"btn btn-info btn-xs\" href=\"javascript:;\" onclick=\"update(" + b.id + ",null,null,"+0+ ")\"> 取消置顶</a>";
                        }
                    }
                } else if (b.checkStatus == 2) {
                    ps = "<a class=\"btn btn-success btn-xs\" href=\"javascript:;\" onclick=\"update(" + b.id + "," + 1 + ",null,null"  + ")\">审核通过</a>";
                    ps += "&nbsp;&nbsp;<a class=\"btn btn-primary btn-xs\" href=\"javascript:;\" onclick=\"update(" + b.id + "," + 3 + ",null,null" + ")\">审核拒绝</a>";

                } else {
                    sj = "";
                }
                if(b.checkStatus != 2&&b.checkStatus != 4){
                    rmStr += "&nbsp;&nbsp;<a class=\"btn btn-primary btn-xs\" href=\"javascript:;\" onclick=\"update(" + b.id + "," + 4 + ",null,null" + ")\">前端移除</a>";
                }
                return ps + sj+rmStr + delStr;
            }
        }],
        method: "get",
        url: url,
        dataType: "json",
        //queryParams: ,
        //返回数据预处理
        responseHandler: function (data) {
            console.log(data);
            var res = data.data;
            return res;
        },
        toolbar: '#toolbar',
        pagination: true,//启用分页条
        sidePagination: "client",
        // pageNumber: 1,
        pageSize: 16,//每页大小
        pageList: [16, 32, 48, 64],//可以选择每页大小
        // showRefresh: true,
        sortable: true,//是否启用排序
        order: "desc",
        clickToSelect: true,//设置true 将在点击行时，自动选择rediobox 和 checkbox
        // search: true,
        // searchText:"",//初始搜索文本

    });

}


//修改 isHome 0不加精 1 加精
function update(id, checkStatus, isHome,isUp) {
    var parm = "";
    /*     if(type == 1 || type == 3){
             parm = "&checkStatus="+type;
         }else if(type==0){
             parm = "&isHome=1";
         }else if(type==4){
             parm = "&isHome=0";
         }*/
/*    parm = "&checkStatus=" + checkStatus + "&isHome=" + isHome;
    if(bonus==1){
        parm+="&bonus="+bonus;
    }*/
    var postData={
        id:id,
        checkStatus:checkStatus,
        isHome:isHome,
        isUp:isUp
    }
    $.ajax({
        type: "POST",
        url: urlcore + "/api/experienceRecommend/updateStatus" ,
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(postData),
        success: function (data) {
            alert("成功");
            $table.bootstrapTable('refresh');
        },
        error: function () {
            alert("失败");
        }
    });

}


//添加商品
function passAll() {
    var selection = $table.bootstrapTable('getSelections');

    if (selection.length == 0) {
        alert("请先选文章");
        return;
    }
    var ids = "";
    for (var item of selection) {
        ids += item.id + ",";
    }
    console.log(selection);
    $.ajax({
        type: "GET",
        url: urlcore + "/api/experienceRecommend/egisAll?ids=" + ids,
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: {},
        success: function (data) {
            alert("审核成功");
            $table.bootstrapTable('refresh');
        },
        error: function () {
            alert("失败");
        }
    });


}

//搜索
function seach() {
    $("#addTo-index-goods").bootstrapTable('destroy');
    var title = $("#title").val();
    var checkStatus = $("#checkStatus").val();
    var url = urlcore + "/api/experienceRecommend/selectNotList?title=" + title + "&checkStatus=" + checkStatus;
    initGroupTable(url);
}

function detailFormatter(index, row) {
    var html = [];
    /*    $.each(row, function (key, value) {
            html.push('<p><b>' + key + ':</b> ' + value + '</p>');
        });*/
    html.push('<p><b>' + "推荐人" + ':</b> ' + row.user.userName + '</p>');
    html.push('<p><b>' + "标题" + ':</b> ' + row.title + '</p>');
    html.push('<p><b>' + "内容" + ':</b> ' + row.content + '</p>');
    if (null != row.pictureUrl) {
        html.push('<p><b>' + "图片" + ':</b></p>');
        var picArr = row.pictureUrl.split(",");
        html.push('<p>');
        for (var zzurl of picArr) {
            html.push('<img style=\"width: 80px;height: 80px;\" src=\"' + zzurl + '\">');
        }
        html.push('</p>');
    }
    if (null != row.goodsList) {
        var liHtml = "";
        for (var g of row.goodsList) {
            liHtml += "<tr class=\"content-row\">\n" +
                "<td><img style=\"width: 50px;height: 50px;\" src=\"" + g.goodsUrl + "\"></td>" +
                "                                    <td>\n" +
                "                                        <div style=\"width: 200px;\"><a href=\"javascript:;\">" +
                g.goodsName +
                "</a></div>" +
                "                                    </td>\n" +
                "                                </tr>";
        }
        var htmlStr =
            "                            <table class=\"table order-list\">\n" +
            "                                <tbody class=\"widget-list-item\">\n" +
            liHtml +
            "                                </tbody>\n" +
            "                            </table>";

        html.push('<p><b>' + "推荐商品" + ':</b> '
            + htmlStr
            + '</p>');
    }

    html.push('<p><b>' + "提交时间" + ':</b> ' + row.gmtDatetime + '</p>');
    return html.join('');
}

//刪除
function doDelete(id) {
    confirm("确认删除吗？", function (callback) {
        if (callback) {
            delCoupon();
        }
    });

    function delCoupon() {
        $.ajax({
            type: "GET",
            url: urlcore + "/api/experienceRecommend/delExp?id=" + id,
            dataType: "json",
            headers: {"Content-Type": "application/json;charset=UTF-8"},
            data: {},
            success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
                if (data.success == true) {
                    $table.bootstrapTable("refresh");
                } else {
                    alert(data.msg);
                }
            },
            error: function () {
                alert("删除失败");
            }
        });
    }

}